package com.lb.shopguide.ui.fragment.boss;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.GuidanceBean;
import com.lb.shopguide.entity.GuideStepBean;
import com.lb.shopguide.entity.shop.StoreBean;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.BossMainActivity;
import com.lb.shopguide.ui.view.ClearTitleBar;
import com.lb.shopguide.ui.view.LbGuideStepLayout;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by kang on 2018/1/19.
 */

public class FragmentRookieGuide extends BaseCommonFragment {
    @BindView(R.id.ctb)
    ClearTitleBar ctb;
    @BindView(R.id.layout_guide_step)
    LinearLayout layoutGuideStep;

    private StoreBean firstStore;
    private boolean popFlag;

    private GuidanceBean mGuidanceBean;


    public static FragmentRookieGuide newInstance(GuidanceBean bean) {
        FragmentRookieGuide fragmentRookieGuide = new FragmentRookieGuide();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.GUIDANCE_BEAN, bean);
        fragmentRookieGuide.setArguments(bundle);
        return fragmentRookieGuide;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_rookie_guide;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.GUIDANCE_BEAN)) {
            mGuidanceBean = (GuidanceBean) bundle.getSerializable(AppConstant.GUIDANCE_BEAN);
        }
        sendRequestGetStoreList();
    }

    @Override
    public void initView() {
        bindStep();
        ctb.setTvTitle("新手引导");
        ctb.setLeftImageSrc(R.drawable.iv_back);
        ctb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((BossMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentRookieGuide.class.getName();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (popFlag) {
            pop();
        }
    }

    public void refreshRookieGuide(GuidanceBean bean) {
        if (!bean.isFinished()) {
            mGuidanceBean = bean;
            bindStep();
            if (mGuidanceBean.getCurrentProcess() == 2) {
                sendRequestGetStoreList();
            }
        } else {
            if (isVisible()) {
                pop();
            } else {
                popFlag = true;
            }
        }
    }

    private void sendRequestGetStoreList() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    JSONObject jsonObject = JsonUtil.getJsonObjectFromMap(response.getReturnContent());
                    try {
                        JSONArray jsonArray = jsonObject.getJSONArray("merStoreList");
                        if (jsonArray.length() != 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                firstStore = JsonUtil.getGson().fromJson(jsonArray.getString(i), StoreBean.class);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        ApiMethodBoss.getStoreList(new SimpleLbObserver(listener, otherListener), this, FragmentEvent.DESTROY_VIEW);
    }

    private void bindStep() {
        if (layoutGuideStep != null) {
            layoutGuideStep.removeAllViews();
            ArrayList<GuideStepBean> list = mGuidanceBean.getMerInfoStepList();
            for (int i = 0; i < list.size(); i++) {
                LbGuideStepLayout layout = new LbGuideStepLayout(mContext);
                layout.setContent(list.get(i), mGuidanceBean.getCurrentProcess());
                layout.setOnStepClickListener(new LbGuideStepLayout.OnStepClickListener() {
                    @Override
                    public void onStepClick(GuideStepBean bean) {
                        if (bean.getId() == mGuidanceBean.getCurrentProcess()) {
                            switch (mGuidanceBean.getCurrentProcess()) {
                                case 1:
                                    start(FragmentShopManage.newInstance());//门店管理
                                    break;
                                case 2://新增店长
//                                start(FragmentAddEditShopManager.newInstance(firstStore.getStoreCode(),AppConstant.STATE_ADD_SHOP_GUIDE));
                                    start(FragmentStoreDetail.newInstance(firstStore));
                                    break;
                                case 3://设置积分比
                                    start(FragmentPointPercent.newInstance());
                                    break;
                                case 4://充值
                                    start(FragmentFinanceManage.newInstance());
                                    break;
                            }
                        }
                    }
                });
                layoutGuideStep.addView(layout);
            }
        }
    }
}
